//
//  OrigPubTitleView.m
//  ShrimpCrowdordering
//
//  Created by jimmy on 2018/8/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrigPubTitleHeaderView.h"

@interface OrigPubTitleHeaderView ()<UITextFieldDelegate>
{
    CGFloat _viewHeight;
}
@property (nonatomic, strong) UITextField * inputTF;

@end

@implementation OrigPubTitleHeaderView


- (id)initWithTitle:(NSString *)title placeholder:(NSString *)placeholder subTitle:(NSString *)subTitle height:(CGFloat)height
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _viewHeight = height;
        [self setUIViewTitle:title placeholder:placeholder subTitle:subTitle];
    }
    return self;
}

- (void)setUIViewTitle:(NSString *)title placeholder:(NSString *)placeholder subTitle:(NSString *)subTitle
{
    UILabel * titleLab = [[UILabel alloc] init];
    titleLab.text = title;
    titleLab.font = [UIFont boldSystemFontOfSize:18.0];
    titleLab.textColor = [UIColor colorWithHexString:@"#212121"];
    [self addSubview:titleLab];
    
    self.inputTF = [[UITextField alloc] init];
    [self addSubview:self.inputTF];
    
    UILabel * subTitleLab = [[UILabel alloc] init];
    subTitleLab.text = title;
    subTitleLab.font = [UIFont systemFontOfSize:12];
    subTitleLab.textColor = [UIColor colorWithHexString:@"#f21934"];
    [self addSubview:subTitleLab];
    
    
    
    titleLab.numberOfLines = 1;    // 指定为单行文本
    // 在单行文本的情况下，参数size设置多少都无所谓，所以里传了CGSizeZero
    CGSize titleSize = [titleLab sizeThatFits:CGSizeZero];
    titleLab.frame = CGRectMake(ScreenX375(16), (_viewHeight - ScreenX375(20))/2, titleSize.width, ScreenX375(20));
    
    subTitleLab.numberOfLines = 1;    // 指定为单行文本
    // 在单行文本的情况下，参数size设置多少都无所谓，所以里传了CGSizeZero
    CGSize subSize = [titleLab sizeThatFits:CGSizeZero];
    subTitleLab.frame = CGRectMake(ScreenX375(20) + titleSize.width, (_viewHeight - ScreenX375(20))/2+ScreenX375(4), subSize.width, ScreenX375(12));
    self.inputTF.frame = CGRectMake(ScreenX375(20) + titleSize.width, (_viewHeight - ScreenX375(20))/2, SCREEN_WIDTH - ScreenX375(32) -titleSize.width , ScreenX375(20));
    
    
    if (placeholder == nil) {
        self.inputTF.hidden = YES;
    }else{
        self.inputTF.delegate = self;
        self.inputTF.hidden = NO;
        self.inputTF.placeholder = placeholder;
    }
    
    if (subTitle == nil || [subTitle isEqualToString:@""]) {
        subTitleLab.hidden = YES;
    }else{
        subTitleLab.hidden = NO;
        subTitleLab.text = subTitle;
    }
}

//- ( BOOL )textField:( UITextField  *)textField shouldChangeCharactersInRange:(NSRange )range replacementString:( NSString  *)string
//{
//    NSLog(@"%@ -- %@", textField.text, self.inputTF.text);
//
//    return YES;
//}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@" %@ -- %@", textField.text, self.inputTF.text);
    if (self.inputChangeBlock) {
        self.inputChangeBlock(self.inputTF.text);
    }
}

- (void)setInputText:(NSString *)text
{
    if (!self.inputTF.isHidden) {
        self.inputTF.text = text;
    }
}


@end
